package edu.unindra.raport.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.unindra.raport.dao.entity.Nilai;
import edu.unindra.raport.dao.impl.NilaiImpl;

import javax.swing.JComboBox;

public class FormDetailMenuNilaiEdit extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JButton btnUbah = null;
	private JButton btnBatal = null;

	private FormDetailMenuNilai parent;

	private JLabel lblNis;
	private JTextField txtNis;

	private NilaiImpl siImpl = null;

	private JLabel lblNip;
	private JTextField txtNip;
	@SuppressWarnings("rawtypes")
	private JComboBox cbKls;
	@SuppressWarnings("rawtypes")
	private JComboBox cbThnAjaran;
	private JLabel lblKls;
	private JLabel lblThnAjaran;
	private JLabel lblTgs;
	private JTextField txtTgs;
	private JLabel lblUlhar;
	private JTextField txtUlhar;
	private JLabel lblUts;
	private JTextField txtUts;
	private JLabel lblUas;
	private JTextField txtUas;
	private JLabel lblMatPel;
	@SuppressWarnings("rawtypes")
	private JComboBox cbMatPel;

	public FormDetailMenuNilaiEdit(FormDetailMenuNilai parent) {
		this.parent = parent;

		siImpl = new NilaiImpl();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Ubah Data Nilai Siswa");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 327);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblNis = new GridBagConstraints();
			gbc_lblNis.gridx = 1;
			gbc_lblNis.anchor = GridBagConstraints.WEST;
			gbc_lblNis.insets = new Insets(2, 2, 5, 5);
			gbc_lblNis.weightx = 0.0D;
			gbc_lblNis.gridy = 0;
			this.lblNis = new JLabel();
			lblNis.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblNis.setText("No Induk Siswa");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0,
					0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
					0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtNis = new GridBagConstraints();
			gbc_txtNis.gridwidth = 3;
			gbc_txtNis.insets = new Insets(0, 0, 5, 5);
			gbc_txtNis.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNis.gridx = 3;
			gbc_txtNis.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtNis);
			this.jContentPane.add(this.lblNis, gbc_lblNis);
			GridBagConstraints gbc_lblNip = new GridBagConstraints();
			gbc_lblNip.anchor = GridBagConstraints.WEST;
			gbc_lblNip.insets = new Insets(0, 0, 5, 5);
			gbc_lblNip.gridx = 1;
			gbc_lblNip.gridy = 1;
			jContentPane.add(getLblNip(), gbc_lblNip);
			GridBagConstraints gbc_txtNip = new GridBagConstraints();
			gbc_txtNip.gridwidth = 3;
			gbc_txtNip.insets = new Insets(0, 0, 5, 5);
			gbc_txtNip.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNip.gridx = 3;
			gbc_txtNip.gridy = 1;
			jContentPane.add(getTxtNip(), gbc_txtNip);
			GridBagConstraints gbc_lblKls = new GridBagConstraints();
			gbc_lblKls.anchor = GridBagConstraints.WEST;
			gbc_lblKls.insets = new Insets(0, 0, 5, 5);
			gbc_lblKls.gridx = 1;
			gbc_lblKls.gridy = 2;
			jContentPane.add(getLblKls(), gbc_lblKls);
			GridBagConstraints gbc_cbKls = new GridBagConstraints();
			gbc_cbKls.gridwidth = 3;
			gbc_cbKls.insets = new Insets(0, 0, 5, 5);
			gbc_cbKls.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbKls.gridx = 3;
			gbc_cbKls.gridy = 2;
			jContentPane.add(getCbKls(), gbc_cbKls);
			GridBagConstraints gbc_lblThnAjaran = new GridBagConstraints();
			gbc_lblThnAjaran.anchor = GridBagConstraints.WEST;
			gbc_lblThnAjaran.insets = new Insets(0, 0, 5, 5);
			gbc_lblThnAjaran.gridx = 1;
			gbc_lblThnAjaran.gridy = 3;
			jContentPane.add(getLblThnAjaran(), gbc_lblThnAjaran);
			GridBagConstraints gbc_cbThnAjaran = new GridBagConstraints();
			gbc_cbThnAjaran.gridwidth = 3;
			gbc_cbThnAjaran.insets = new Insets(0, 0, 5, 5);
			gbc_cbThnAjaran.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbThnAjaran.gridx = 3;
			gbc_cbThnAjaran.gridy = 3;
			jContentPane.add(getCbThnAjaran(), gbc_cbThnAjaran);
			GridBagConstraints gbc_lblTgs = new GridBagConstraints();
			gbc_lblTgs.anchor = GridBagConstraints.WEST;
			gbc_lblTgs.insets = new Insets(0, 0, 5, 5);
			gbc_lblTgs.gridx = 1;
			gbc_lblTgs.gridy = 4;
			jContentPane.add(getLblTgs(), gbc_lblTgs);
			GridBagConstraints gbc_txtTgs = new GridBagConstraints();
			gbc_txtTgs.gridwidth = 3;
			gbc_txtTgs.insets = new Insets(0, 0, 5, 5);
			gbc_txtTgs.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTgs.gridx = 3;
			gbc_txtTgs.gridy = 4;
			jContentPane.add(getTxtTgs(), gbc_txtTgs);
			GridBagConstraints gbc_lblUlhar = new GridBagConstraints();
			gbc_lblUlhar.insets = new Insets(0, 0, 5, 5);
			gbc_lblUlhar.gridx = 1;
			gbc_lblUlhar.gridy = 5;
			jContentPane.add(getLblUlhar(), gbc_lblUlhar);
			GridBagConstraints gbc_txtUlhar = new GridBagConstraints();
			gbc_txtUlhar.gridwidth = 3;
			gbc_txtUlhar.insets = new Insets(0, 0, 5, 5);
			gbc_txtUlhar.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtUlhar.gridx = 3;
			gbc_txtUlhar.gridy = 5;
			jContentPane.add(getTxtUlhar(), gbc_txtUlhar);
			GridBagConstraints gbc_lblUts = new GridBagConstraints();
			gbc_lblUts.anchor = GridBagConstraints.WEST;
			gbc_lblUts.insets = new Insets(0, 0, 5, 5);
			gbc_lblUts.gridx = 1;
			gbc_lblUts.gridy = 6;
			jContentPane.add(getLblUts(), gbc_lblUts);
			GridBagConstraints gbc_txtUts = new GridBagConstraints();
			gbc_txtUts.gridwidth = 3;
			gbc_txtUts.insets = new Insets(0, 0, 5, 5);
			gbc_txtUts.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtUts.gridx = 3;
			gbc_txtUts.gridy = 6;
			jContentPane.add(getTxtUts(), gbc_txtUts);
			GridBagConstraints gbc_lblUas = new GridBagConstraints();
			gbc_lblUas.anchor = GridBagConstraints.WEST;
			gbc_lblUas.insets = new Insets(0, 0, 5, 5);
			gbc_lblUas.gridx = 1;
			gbc_lblUas.gridy = 7;
			jContentPane.add(getLblUas(), gbc_lblUas);
			GridBagConstraints gbc_txtUas = new GridBagConstraints();
			gbc_txtUas.gridwidth = 3;
			gbc_txtUas.insets = new Insets(0, 0, 5, 5);
			gbc_txtUas.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtUas.gridx = 3;
			gbc_txtUas.gridy = 7;
			jContentPane.add(getTxtUas(), gbc_txtUas);
			GridBagConstraints gbc_lblMatPel = new GridBagConstraints();
			gbc_lblMatPel.anchor = GridBagConstraints.WEST;
			gbc_lblMatPel.insets = new Insets(0, 0, 5, 5);
			gbc_lblMatPel.gridx = 1;
			gbc_lblMatPel.gridy = 8;
			jContentPane.add(getLblMatPel(), gbc_lblMatPel);
			GridBagConstraints gbc_cbMatPel = new GridBagConstraints();
			gbc_cbMatPel.gridwidth = 3;
			gbc_cbMatPel.insets = new Insets(0, 0, 5, 5);
			gbc_cbMatPel.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbMatPel.gridx = 3;
			gbc_cbMatPel.gridy = 8;
			jContentPane.add(getCbMatPel(), gbc_cbMatPel);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 9;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnUbah = new GridBagConstraints();
			gbc_btnUbah.gridx = 6;
			gbc_btnUbah.insets = new Insets(2, 2, 2, 0);
			gbc_btnUbah.anchor = 13;
			gbc_btnUbah.gridy = 9;
			this.jContentPane.add(getBtnUbah(), gbc_btnUbah);
		}
		return this.jContentPane;
	}

	private JButton getBtnUbah() {
		if (this.btnUbah == null) {
			this.btnUbah = new JButton();
			this.btnUbah.setText("Ubah");
			this.btnUbah.setToolTipText("Insert");
			this.btnUbah.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int retVal = 0;
					Nilai si = new Nilai();

					si.setNis(txtNis.getText());
					si.setNis(txtNip.getText());
					si.setKls((cbKls.getSelectedItem().toString()));
					si.setThn_ajaran((cbThnAjaran.getSelectedItem().toString()));
					si.setTgs(txtTgs.getText());
					si.setUlhar(txtUlhar.getText());
					si.setUts(txtUts.getText());
					si.setUas(txtUas.getText());
					si.setMat_pel(cbMatPel.getSelectedItem().toString());

					try {
						retVal = siImpl.update(si);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

					if (retVal > -1) {
						JOptionPane.showMessageDialog(
								FormDetailMenuNilaiEdit.this,
								"Proses update data nilai siswa  berhasil");
						int i = parent.getTable().convertRowIndexToModel(
								parent.getTable().getSelectedRow());

						parent.getModelTable().setNilaiAt(si, i);

						FormDetailMenuNilaiEdit.this.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(
								FormDetailMenuNilaiEdit.this,
								"Proses update data siswa  tidak berhasil");
					}
				}
			});
		}
		return this.btnUbah;
	}

	JTextField getTxtCardNumber() {
		if (txtNis == null) {
			txtNis = new JTextField();
			txtNis.setColumns(10);

			txtNis.setEnabled(false);
		}
		return txtNis;
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.setToolTipText("Insert");
			btnBatal.setText("Batal");
			btnBatal.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					FormDetailMenuNilaiEdit.this.setVisible(false);
				}
			});
		}
		return btnBatal;
	}

	public void setNilai(Nilai e) {
		if (e == null) {
			throw new IllegalArgumentException();
		}

		txtNis.setText(e.getNis());
		txtNip.setText(e.getNip());
		cbKls.getModel().setSelectedItem(e.getKls());
		cbThnAjaran.getModel().setSelectedItem(e.getThn_ajaran());
		txtTgs.setText(e.getTgs());
		txtUlhar.setText(e.getUlhar());
		txtUts.setText(e.getUts());
		txtUas.setText(e.getUas());
		cbMatPel.getModel().setSelectedItem(e.getMat_pel());
	}

	private JLabel getLblNip() {
		if (lblNip == null) {
			lblNip = new JLabel();
			lblNip.setText("No Induk Guru");
			lblNip.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblNip;
	}

	private JTextField getTxtNip() {
		if (txtNip == null) {
			txtNip = new JTextField();
			txtNip.setEditable(false);
			txtNip.setColumns(10);
		}
		return txtNip;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbKls() {
		if (cbKls == null) {
			cbKls = new JComboBox();
			cbKls.setModel(new DefaultComboBoxModel(new String[] { "1", "2",
					"3", "4", "5", "6" }));
		}
		return cbKls;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbThnAjaran() {
		if (cbThnAjaran == null) {
			cbThnAjaran = new JComboBox();
			cbThnAjaran.setModel(new DefaultComboBoxModel(new String[] {
					"2005", "2006", "2007", "2008", "2009", "2010", "2011",
					"2012", "2013", "2014", "2015", "2016", "2017" }));
		}
		return cbThnAjaran;
	}

	private JLabel getLblKls() {
		if (lblKls == null) {
			lblKls = new JLabel();
			lblKls.setText("Kelas");
		}
		return lblKls;
	}

	private JLabel getLblThnAjaran() {
		if (lblThnAjaran == null) {
			lblThnAjaran = new JLabel();
			lblThnAjaran.setText("Tahun Ajaran");
		}
		return lblThnAjaran;
	}

	private JLabel getLblTgs() {
		if (lblTgs == null) {
			lblTgs = new JLabel();
			lblTgs.setText("Nilai Tugas");
			lblTgs.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblTgs;
	}

	private JTextField getTxtTgs() {
		if (txtTgs == null) {
			txtTgs = new JTextField();
			txtTgs.setColumns(10);
		}
		return txtTgs;
	}

	private JLabel getLblUlhar() {
		if (lblUlhar == null) {
			lblUlhar = new JLabel();
			lblUlhar.setText("Nilai Ulangan Harian");
			lblUlhar.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblUlhar;
	}

	private JTextField getTxtUlhar() {
		if (txtUlhar == null) {
			txtUlhar = new JTextField();
			txtUlhar.setColumns(10);
		}
		return txtUlhar;
	}

	private JLabel getLblUts() {
		if (lblUts == null) {
			lblUts = new JLabel();
			lblUts.setText("Nilai Uts");
			lblUts.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblUts;
	}

	private JTextField getTxtUts() {
		if (txtUts == null) {
			txtUts = new JTextField();
			txtUts.setColumns(10);
		}
		return txtUts;
	}

	private JLabel getLblUas() {
		if (lblUas == null) {
			lblUas = new JLabel();
			lblUas.setText("Nilai Uas");
			lblUas.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblUas;
	}

	private JTextField getTxtUas() {
		if (txtUas == null) {
			txtUas = new JTextField();
			txtUas.setColumns(10);
		}
		return txtUas;
	}

	private JLabel getLblMatPel() {
		if (lblMatPel == null) {
			lblMatPel = new JLabel();
			lblMatPel.setText("Mata Pelajaran");
			lblMatPel.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return lblMatPel;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbMatPel() {
		if (cbMatPel == null) {
			cbMatPel = new JComboBox();
			cbMatPel.setModel(new DefaultComboBoxModel(new String[] {
					"Bahasa Indonesia", "Bahasa Inggris", "Matematika",
					"Ilmu Pengetahuan Alam", "Ilmu Pengetahuan Sosial" }));
		}
		return cbMatPel;
	}
}